package com.ufaud.ais.pub.view;

import java.util.List;

/**
 * 功能：分页查询结果封装类
 * 
 * @author 林可
 * @version 1.0 2010-6-25
 */
public class PageBean<T> {
	
	/**
	 * 计算所取数据集的偏移量
	 * 
	 * @param pageIndex
	 * @param pageSize
	 * @return 数据偏移量
	 */
	public static int getOffset(int pageIndex, int pageSize) {
		return pageSize * (pageIndex - 1);
	}
	
	/**
	 * 功能: 工厂函数，根据传入参数返回一个pageBean的实例
	 * @author 林可  2010-8-9 上午09:17:16
	 * @param <U>
	 * @param resultList 结果列表
	 * @param pageIndex 当前页码
	 * @param pageSize 每页大小
	 * @param totalCount 总结果数
	 * @return
	 */
	public static <U> PageBean<U>  getInstance(List<U> resultList, int pageIndex, int pageSize, int totalCount) {
		return new PageBean<U>(resultList, pageIndex, pageSize, totalCount);
	}
	
	private PageBean(List<T> resultList, int pageIndex, int pageSize, int totalCount) {
		this.resultList = resultList;
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
		this.totalCount = totalCount;
		init();
	}

	// 需要设置进来的结果
	private List<T> resultList; // 要返回的记录
	private int pageIndex; // 当前页码
	private int pageSize; // 每页记录数
	private int totalCount; // 总记录数
	
	// 计算出来的结果
	private int totalPage; // 总页数
	private boolean isFirstPage;
	private boolean isLastPage;
	private boolean hasPreviousPage;
	private boolean hasNextPage;

	private void init() {
		totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
		isFirstPage = pageIndex == 1 ? true : false;
		isLastPage = pageIndex == totalPage ? true : false;
		hasPreviousPage = !isFirstPage;
		hasNextPage = !isLastPage;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public boolean isFirstPage() {
		return isFirstPage;
	}

	public boolean isLastPage() {
		return isLastPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public List<T> getResultList() {
		return resultList;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public int getTotalPage() {
		return totalPage;
	}

}
